<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

if($_RQ['op']=='list'){
	$type=$this->plugin('jy_weishop')->fc_type__template();
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_message_template',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	//$return['type']=array('1'=>'进云物联打印机','2'=>'进云物联打印机new','3'=>'进云物联打印机V9');		
	//$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
			'type'=>'list',
			'tabs'=>array(
				array('op'=>'list','title'=>'微信模板管理'),
				array('op'=>'edit','title'=>'添加模板'),
			),
			'list'=>array('data'=>'list','list'=>array(
				array('name'=>'title','title'=>'模板名称','type'=>'string'),
				
			),
			),
		);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	
	if($_RQ['id']){
		$item=pdo_get('jy_weishop_message_template',array('id'=>$_RQ['id']));
		$item['data']=json_decode($item['data'],true);
	}else{
		$item=array();
	}
	/*$item['daohang']=json_encode($item['daohang'],true);
	$type=$this->type__template();
    $return['group1']=$type;
	$return['group2']=$type['1']['v'];
	$return['item']=$item;
	$return['messagetype']=array('1'=>'发送模板消息','2'=>'发送客服消息','3'=>'发送混合消息');
	$return['status']=array('1'=>'开启','0'=>'关闭');
	*/
	$result = $this->tmessage__getlist();
		if (!(empty($result['errcode']))) 
		{
			exi($result['errmsg'],'error');
		}
		foreach ($result['template_list'] as $key => &$value ) 
		{	$value['id']=$key;
			preg_match_all('{{(.)*?}}', $value['content'], $matches);
			
			preg_match_all('/[\x7f-\xff]+/', $value['content'], $matches2);
			
			foreach ($matches[0] as &$v ) 
			{
				$v = str_replace(array('{', '}', '.DATA'), '', $v);
			}
			unset($v);
			$value['contents'] = $matches[0];
			$value['contents2'] = $matches2[0];
			$result['template_list'][$key]['content'] = str_replace(array("\n\n", "\n"), '<br />', $value['content']);
			$result['template_list'][$key]['content2']=$value['contents2'];
			$result['template_list'][$key]['num']=count($value['contents2']);
			$result['template_list'][$key]['m']=$value['content'];
		}
		unset($value);
		
		$return['tpl']=$result['template_list'];
		$return['item']=$item;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('messagetype','title','send_desc','typecode','typegroup','template_id','first','firstcolor','remark','remarkcolor'),$postdata);	
	$data['data']=array('keyword1'=>$postdata['keyword1'],'keyword2'=>$postdata['keyword2'],'keyword3'=>$postdata['keyword3'],'keyword4'=>$postdata['keyword4'],'keyword5'=>$postdata['keyword5']);
	$data['data']=json_encode($data['data']);
	
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_message_template',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				 $data['createtime']=TIMESTAMP;
				pdo_insert('jy_weishop_message_template',$data);
				exi('添加成功！','','list');
			}
			}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_message_template',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_message_template',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>